﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using FaceSharp.Api;
using FaceSharp.Api.GraphApi;
using FaceSharp.Api.Types;
using FaceSharp.MVC.Code.Facebook;
using FaceSharp.MVC.Filters;
using NUnit.Framework;
using Newtonsoft.Json;
using Types = FaceSharp.Api.Types;
namespace FaceSharp.MVC.Tests
{
    [TestFixture]
    public class Class1
    {
        private const string Coookie ="UB5Mdwp1u6S2zCtQQE1KSe5LtQTFTKxhWkx1lS4OyuQ.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiIyLkFRRFNGaG1rbmRCTXF6dWguMzYwMC4xMzIwOTE1NjAwLjEtNTA0MTI1NDQzfHFkT1FVcVZKX0F1aktRYVduLUJwUEJNYzhmYyIsImlzc3VlZF9hdCI6MTMyMDkwOTUxMSwidXNlcl9pZCI6IjUwNDEyNTQ0MyJ9";
        private const string Secret = "d7881207911dfc701643f8012128d038";
        [Test]
        public void ValidateSignedReqeust()
        {
            var secret = "secret";
            var signature = "vlXgu64BQGFSQrY0ZcJBZASMvYvTHu9GQ0YM9rjPSso";
            var payload = "eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsIjAiOiJwYXlsb2FkIn0";

            var facebookGraphApiFilter = new FacebookGraphApiFilter();
            var results = facebookGraphApiFilter.ValidatePayloadSignature(secret, signature, payload);
            Assert.That(results,Is.True);
         }

        [Test]
        public void Paininmyass()
        {
            var container = new WindsorContainer();
            container.Register(Component.For<IFacebookCore>().ImplementedBy<FacebookCore>());
            var _facebookCore = (IFacebookCore)container.Resolve(typeof(IFacebookCore));
            
          //  container.AddComponent("IFacebookCore", typeof(FacebookCore));
            
        }

        [Test] 
        public void DeserializeJsonPlaces()
        {
            const string json = "";
            var results = new TypedGraphBase<PlaceType>(new FacebookCore()).Deserialize(json);
            int i = 1 + 1;
        }

        [Test]
        public void DecdodeJson()
        {
            var signedRequest =
                "eBsWSapijHegYRoOLUvX_6l5f3G6w84eczKNf-j8q6w.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEzMjcxNzYwMDAsImlzc3VlZF9hdCI6MTMyNzE3MTMzMCwib2F1dGhfdG9rZW4iOiJBQUFDWkFPNTQzWkNaQThCQU5Ra0ZaQ0hTdHJyT0t0RDNwWEg1cXdwdU5TM3BHUzJ1VlFNQVduQ0FoM1pBeEpWRkpMSjlSWGJ2VkRNdXhUTHIxeVZTbnV1MVNPVnowNE9YN0JXZG1aQ1UyVzJ3WkRaRCIsInBhZ2UiOnsiaWQiOiIyMzI0MDgzODAxNjc4NzMiLCJsaWtlZCI6dHJ1ZSwiYWRtaW4iOnRydWV9LCJ1c2VyIjp7ImNvdW50cnkiOiJ1cyIsImxvY2FsZSI6ImVuX1VTIiwiYWdlIjp7Im1pbiI6MjF9fSwidXNlcl9pZCI6IjUwNDEyNTQ0MyJ9";

            //Coookie;
            var encodedJson = signedRequest.Split('.')[1];
            var facebookGraphApiFilter = new FacebookGraphApiFilter();
            var results = facebookGraphApiFilter.DecodePayload(encodedJson);

            Console.WriteLine(results);
            Assert.That(results,
                        Is.EqualTo(
                            "{\"algorithm\":\"HMAC-SHA256\",\"expires\":1327176000,\"issued_at\":1327171330,\"oauth_token\":\"AAACZAO543ZCZA8BANQkFZCHStrrOKtD3pXH5qwpuNS3pGS2uVQMAWnCAh3ZAxJVFJLJ9RXbvVDMuxTLr1yVSnuu1SOVz04OX7BWdmZCU2W2wZDZD\",\"page\":{\"id\":\"232408380167873\",\"liked\":true,\"admin\":true},\"user\":{\"country\":\"us\",\"locale\":\"en_US\",\"age\":{\"min\":21}},\"user_id\":\"504125443\"}"));
        }



        [Test]
        public void GetSignedRequest()
        {
            const string appId = "168481336589727";
            const string appSecret = "154d7465c23bda8de5a47191e723d07e";
            const string authPayload = "eBsWSapijHegYRoOLUvX_6l5f3G6w84eczKNf-j8q6w.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEzMjcxNzYwMDAsImlzc3VlZF9hdCI6MTMyNzE3MTMzMCwib2F1dGhfdG9rZW4iOiJBQUFDWkFPNTQzWkNaQThCQU5Ra0ZaQ0hTdHJyT0t0RDNwWEg1cXdwdU5TM3BHUzJ1VlFNQVduQ0FoM1pBeEpWRkpMSjlSWGJ2VkRNdXhUTHIxeVZTbnV1MVNPVnowNE9YN0JXZG1aQ1UyVzJ3WkRaRCIsInBhZ2UiOnsiaWQiOiIyMzI0MDgzODAxNjc4NzMiLCJsaWtlZCI6dHJ1ZSwiYWRtaW4iOnRydWV9LCJ1c2VyIjp7ImNvdW50cnkiOiJ1cyIsImxvY2FsZSI6ImVuX1VTIiwiYWdlIjp7Im1pbiI6MjF9fSwidXNlcl9pZCI6IjUwNDEyNTQ0MyJ9";
            var facebookGraphApiFilter = new FacebookGraphApiFilter();
            var signedRequest = facebookGraphApiFilter.GetSignedRequest(authPayload,appSecret);
            Assert.That(signedRequest,Is.Not.Null);
            Assert.That(signedRequest.Algorithm, Is.EqualTo("HMAC-SHA256"));
            Assert.That(signedRequest.Expires, Is.EqualTo(1327176000));
            Assert.That(signedRequest.IssuedAt, Is.EqualTo(1327171330));
            Assert.That(signedRequest.OAuthToken, Is.EqualTo("AAACZAO543ZCZA8BANQkFZCHStrrOKtD3pXH5qwpuNS3pGS2uVQMAWnCAh3ZAxJVFJLJ9RXbvVDMuxTLr1yVSnuu1SOVz04OX7BWdmZCU2W2wZDZD"));
            Assert.That(signedRequest.UserId, Is.EqualTo(504125443));
            Assert.That(signedRequest.Page, Is.Not.Null);
            Assert.That(signedRequest.Page.Id, Is.EqualTo(232408380167873));
            Assert.That(signedRequest.Page.Liked, Is.True);
            Assert.That(signedRequest.User, Is.Not.Null);
            Assert.That(signedRequest.User.Country, Is.EqualTo("us"));
            Assert.That(signedRequest.User.Locale, Is.EqualTo("en_US"));
            Assert.That(signedRequest.User.Age, Is.Not.Null);
            Assert.That(signedRequest.User.Age.Max.HasValue, Is.False);
            Assert.That(signedRequest.User.Age.Min.HasValue, Is.True);
            Assert.That(signedRequest.User.Age.Min.GetValueOrDefault(0), Is.EqualTo(21));
        }


        [Test]
        public void ValidateSignedRequest()
        {
            const string appId = "168481336589727";
            const string appSecret = "154d7465c23bda8de5a47191e723d07e";
            const string authPayload = "eBsWSapijHegYRoOLUvX_6l5f3G6w84eczKNf-j8q6w.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEzMjcxNzYwMDAsImlzc3VlZF9hdCI6MTMyNzE3MTMzMCwib2F1dGhfdG9rZW4iOiJBQUFDWkFPNTQzWkNaQThCQU5Ra0ZaQ0hTdHJyT0t0RDNwWEg1cXdwdU5TM3BHUzJ1VlFNQVduQ0FoM1pBeEpWRkpMSjlSWGJ2VkRNdXhUTHIxeVZTbnV1MVNPVnowNE9YN0JXZG1aQ1UyVzJ3WkRaRCIsInBhZ2UiOnsiaWQiOiIyMzI0MDgzODAxNjc4NzMiLCJsaWtlZCI6dHJ1ZSwiYWRtaW4iOnRydWV9LCJ1c2VyIjp7ImNvdW50cnkiOiJ1cyIsImxvY2FsZSI6ImVuX1VTIiwiYWdlIjp7Im1pbiI6MjF9fSwidXNlcl9pZCI6IjUwNDEyNTQ0MyJ9";
            User user;
            var facebookGraphApiFilter = new FacebookGraphApiFilter();
            var signedRequest = facebookGraphApiFilter.GetSignedRequest(authPayload,appSecret);
            var validationProcess = facebookGraphApiFilter.ValidateSignedRequest(appId, appSecret, signedRequest,out user);
            int i = 0;
        }



        [Test]
        public void EncodeJson()
        {
            var json = "{\"algorithm\":\"HMAC-SHA256\",\"code\":\"2.AQDSFhmkndBMqzuh.3600.1320915600.1-504125443|qdOQUqVJ_AujKQaWn-BpPBMc8fc\",\"issued_at\":1320909511,\"user_id\":\"504125443\"}";
            var facebookGraphApiFilter = new FacebookGraphApiFilter();
            var results = facebookGraphApiFilter.EncodePayload(json);

            var signedRequest = Coookie; 
            var encodedJson = signedRequest.Split('.')[1];
            
            Console.WriteLine(results);
            Assert.That(results, Is.EqualTo(encodedJson));
        }

        //[Test]
        //public void ConfirmHash2()
        //{
        //    //var signedRequest = Coookie; //"A7k93vk7Txu98zfsYquJw5poiJzxgJQD1K3nZKHwg6E.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiIyLkFRQVc0X25EU05lY3JKajcuMzYwMC4xMzIwODYxNjAwLjEtNjgxNzg1NjQ4fG9zT3RhbjF2Vk5qN3lWRGFoQ3BsSkhlZllyOCIsImlzc3VlZF9hdCI6MTMyMDg1NDU1MCwidXNlcl9pZCI6IjY4MTc4NTY0OCJ9";
        //    //var payload = signedRequest.Split('.')[1];
        //    //var signature = (signedRequest.Split('.')[0]);
        //    var facebookGraphApiFilter = new FacebookGraphApiFilter();
        //    string code;
        //    long uid;
        //    var results = facebookGraphApiFilter.V(Secret, Coookie,out code,out uid);
        //    Assert.That(results);
        //}

        [Test]
        public void ConvirmHash()
        {
            var signedRequest = Coookie; //"A7k93vk7Txu98zfsYquJw5poiJzxgJQD1K3nZKHwg6E.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiIyLkFRQVc0X25EU05lY3JKajcuMzYwMC4xMzIwODYxNjAwLjEtNjgxNzg1NjQ4fG9zT3RhbjF2Vk5qN3lWRGFoQ3BsSkhlZllyOCIsImlzc3VlZF9hdCI6MTMyMDg1NDU1MCwidXNlcl9pZCI6IjY4MTc4NTY0OCJ9";
            var encodedJson = signedRequest.Split('.')[1];
            var facebookGraphApiFilter = new FacebookGraphApiFilter();
            var cookieHash = (signedRequest.Split('.')[0]);
            var results = facebookGraphApiFilter.DecodePayload(encodedJson);

            var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(Secret));
            //var md5 = MD5CryptoServiceProvider.Create();
            var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(encodedJson)); //ApplicationSecret
            var hashString = Convert.ToBase64String(hash).Replace("=", string.Empty); //.ToLower();

            Assert.That(hashString, Is.EqualTo(cookieHash));

            Console.WriteLine(results);
           // Assert.That(results, Is.EqualTo("{\"algorithm\":\"HMAC-SHA256\",\"code\":\"2.AQCkGLI5shrlbnh3.3600.1320732000.1-504125443|p1pWx52EJnnD-_R4TpxPPZ2Oy_U\",\"issued_at\":1320725893,\"user_id\":\"504125443\"}"));
        }

        [Test]
        public void ValidateCookieValue()
        {
            var cookieValue = "A7k93vk7Txu98zfsYquJw5poiJzxgJQD1K3nZKHwg6E.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiIyLkFRQVc0X25EU05lY3JKajcuMzYwMC4xMzIwODYxNjAwLjEtNjgxNzg1NjQ4fG9zT3RhbjF2Vk5qN3lWRGFoQ3BsSkhlZllyOCIsImlzc3VlZF9hdCI6MTMyMDg1NDU1MCwidXNlcl9pZCI6IjY4MTc4NTY0OCJ9";
            var facebookGraphApiFilter = new FacebookGraphApiFilter();
          //  var results = facebookGraphApiFilter.ValidateAccessTokencookieValue,"");

           // Console.WriteLine(results);
          //  Assert.That(results, Is.EqualTo(true));
        }

        [Test]
        public  void ParseSignedRequest()
        {
            var signedRequestObj = JsonConvert.DeserializeObject<Types.SignedRequest>("{\"algorithm\":\"HMAC-SHA256\",\"expires\":1298566800,\"issued_at\":1298561452,\"oauth_token\":\"123419921037393|2.Jc8_ztPAeOE_c_0o7cL9nA__.3600.1298566800-504125443|CgX85DVDQJAxwMZMJ6AyXBEjVqU\",\"page\":{\"id\":\"123419921037393\",\"liked\":true,\"admin\":true},\"user\":{\"country\":\"us\",\"locale\":\"en_US\",\"age\":{\"min\":21}},\"user_id\":\"504125443\"}");
            Console.WriteLine(signedRequestObj.Page.Liked );
            Assert.That(signedRequestObj, Is.Not.Null);
        }



    }
}
